<?php

class bookmark {

    private $mysql_connection = NULL;

    public function __construct($mysql_connection) {
        $this->mysql_connection = $mysql_connection;
    }
    
    public function add_bookmark($category, $name, $url, $comment) {
    
        $this->mysql_connection->query("INSERT INTO bookmarks (id, category, name, url, comment) VALUES ('', %s, %s, %s, %s);", $category, $name, $url, $comment);
        return 1;
    }
    
    public function check_dupicate_b($column, $value) {

        $this->mysql_connection->query("SELECT * FROM bookmarks WHERE $column = %s;", $value);
        if($this->mysql_connection->num_rows())
            return 0;
        return 1;
    }

    public function delete_bookmark($id) {
    
        $this->mysql_connection->query("DELETE FROM bookmarks WHERE id = %s;", $id);
        return 1;
    }
    
    public function move_to_category($id, $new_cat) {
    
        $this->mysql_connection->query("UPDATE bookmarks SET category = %s WHERE id = %s;", $new_cat, $id);
        return 1;
    }
    
    public function edit_bookmark($id, $category, $name, $url, $comment) {
    
        $this->mysql_connection->query("UPDATE bookmarks SET category = %s, name = %s, url = %s, comment = %s WHERE id = %s;", $category, $name, $url, $comment, $id);
        return 1;
    }

    public function check_dupicate_c($name) {

        $this->mysql_connection->query("SELECT id FROM bookmark_categorys WHERE name = %s;", $name);
        if(!$this->mysql_connection->num_rows())
            return 0;
        $row = $this->mysql_connection->fetch();
        return $row['id'];
    }

    public function add_category($name, $description) {
    
        $this->mysql_connection->query("INSERT INTO bookmark_categorys (id, name, description) VALUES ('', %s, %s);", $name, $description);
        return 1;
    }
    
    public function edit_category($id, $name, $description) {
    
        $this->mysql_connection->query("UPDATE bookmark_categorys SET name = %s, description = %s WHERE id = %s;", $name, $description, $id);
        return 1;
    }
    
    public function delete_category($id) {
    
        $this->mysql_connection->query("DELETE FROM bookmark_categorys WHERE id = %s;", $id);
        return 1;
    }
    
    public function search_category($name) {
    
        $this->mysql_connection->query("SELECT * FROM bookmark_categorys WHERE name LIKE '%$name%' LIMIT 3;");
        
        if($this->mysql_connection->num_rows()) {
            for($i;$tmp = $this->mysql_connection->fetch();$i++) {
                $row[$i]['id'] = $tmp['id'];
                $row[$i]['name'] = $tmp['name'];
                $row[$i]['description'] = $tmp['description'];
            }
            return $row;
        } else {
            return "";
        }
    }
    
    public function get_all_category_names() {
    
        $this->mysql_connection->query("SELECT * FROM bookmark_categorys LIMIT 3;");
        if($this->mysql_connection->num_rows()) {
            for($i;$tmp = $this->mysql_connection->fetch();$i++) {
                $row[$i]['id'] = $tmp['id'];
                $row[$i]['name'] = $tmp['name'];
                $row[$i]['description'] = $tmp['description'];
            }
            return $row;
        } else {
            return "";
        }
    }
}

?>